﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crazycrash.Interface;
namespace Crazycrash.UI
{
    public partial class Gamepanel : UserControl
    {
        public AnimalControl animalcontrol = new AnimalControl();
        public static Animalpanel[,] animalpanels = new Animalpanel[8, 8];
        public static Animalpanel firstselected;
        public static Animalpanel secondselected;
        public Int16 SIZE = 40;
        public Gamepanel()
        {
            InitializeComponent();
        }
        private void Gamepanel_Load(object sender, EventArgs e)
        {
            animalcontrol.generate();
            Adjust();
            mapImages(animalpanels, animalcontrol);
        }
        private void Adjust()//重新生成animalpanels
        {
            this.BackColor = Color.Transparent;
            for (int hor = 0; hor <= 7; hor++)                //将现在的animalpanels整体向下移动
            {
                for (int vert = 7; vert >= 0; vert--)
                {
                    if (animalpanels[hor, vert] == null)
                    {
                        int tempindex = vert;
                        while (tempindex >= 0 && animalpanels[hor, tempindex] == null)
                        {
                            tempindex -= 1;
                        }
                        if (tempindex >= 0)
                        {
                            animalpanels[hor, vert] = animalpanels[hor, tempindex];
                            animalpanels[hor, tempindex] = null;
                            animalpanels[hor, vert].x = hor;
                            animalpanels[hor, vert].y = vert;
                        }
                    }
                }
            }               
            for (int hor = 0; hor <= 7; hor++)                
            {
                int templocation = 0;
                for (int vert = 7; vert >=0; vert--)                
                {               
                    if (animalpanels[hor, vert] == null)//生成所有的animalpanels                    
                    {
                        templocation--;
                        animalpanels[hor, vert] = new Animalpanel();                        
                        animalpanels[hor, vert].x = hor;                        
                        animalpanels[hor, vert].y = vert;
                        animalpanels[hor, vert].Location = new Point(hor * this.SIZE, templocation * this.SIZE);
                        animalpanels[hor, vert].fatherpanel = this;
                    }   
                }    
            }   
            for (int i = 0; i <= 7; i++)   
            {
                for (int j = 0; j <= 7; j++)
                {
                    animalpanels[i, j].fatherpanel = this;
                    this.Controls.Add(animalpanels[i, j]);   
                }   
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            bool isfinished=true;
            for (int y = 7; y >= 0; y--)
            {
                for (int x = 0; x <= 7; x++)
                {
                    if (animalpanels[x, y].Location.Y != y * this.SIZE)
                    {
                        animalpanels[x, y].Location = new Point(x * SIZE, animalpanels[x, y].Location.Y + 10);
                        isfinished = false;
                    }
                }
            }
            if (isfinished)
            {
                Movedowmtimer.Stop();
            }
        }
        public void swapback2tiles()
        {
            int tempx = Gamepanel.secondselected.x;
            int tempy = Gamepanel.secondselected.y;
            Gamepanel.secondselected.x = Gamepanel.firstselected.x;
            Gamepanel.secondselected.y = Gamepanel.firstselected.y;
            Gamepanel.firstselected.x = tempx;
            Gamepanel.firstselected.y = tempy;
            Gamepanel.animalpanels[Gamepanel.firstselected.x, Gamepanel.firstselected.y] = Gamepanel.secondselected;
            Gamepanel.animalpanels[Gamepanel.secondselected.x, Gamepanel.secondselected.y] = Gamepanel.firstselected;
            if (Gamepanel.firstselected.x == Gamepanel.secondselected.x - 1)
            {
                while (Gamepanel.firstselected.Location.X != Gamepanel.firstselected.x * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X - 1, Gamepanel.firstselected.Location.Y);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X + 1, Gamepanel.secondselected.Location.Y);
                }
            }
            else if (Gamepanel.firstselected.x == Gamepanel.secondselected.x + 1)
            {
                while (Gamepanel.firstselected.Location.X != Gamepanel.firstselected.x * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X + 1, Gamepanel.firstselected.Location.Y);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X - 1, Gamepanel.secondselected.Location.Y);
                }
            }
            else if (Gamepanel.firstselected.y == Gamepanel.secondselected.y + 1)
            {
                while (Gamepanel.firstselected.Location.Y != Gamepanel.firstselected.y * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X, Gamepanel.firstselected.Location.Y + 1);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X, Gamepanel.secondselected.Location.Y - 1);
                }
            }
            else if (Gamepanel.firstselected.y == Gamepanel.secondselected.y - 1)
            {
                while (Gamepanel.firstselected.Location.Y != Gamepanel.firstselected.y * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X, Gamepanel.firstselected.Location.Y - 1);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.firstselected.Location.X, Gamepanel.secondselected.Location.Y + 1);
                }
            }
            Gamepanel.firstselected = null;
            Gamepanel.secondselected = null;
        }
        public void swap2tiles()
        {
            Gamepanel.animalpanels[Gamepanel.firstselected.x, Gamepanel.firstselected.y] = Gamepanel.secondselected;
            Gamepanel.animalpanels[Gamepanel.secondselected.x, Gamepanel.secondselected.y] = Gamepanel.firstselected;
            int tempx = Gamepanel.secondselected.x;
            int tempy = Gamepanel.secondselected.y;
            Gamepanel.secondselected.x = Gamepanel.firstselected.x;
            Gamepanel.secondselected.y = Gamepanel.firstselected.y;
            Gamepanel.firstselected.x = tempx;
            Gamepanel.firstselected.y = tempy;
            animalcontrol.swap(new Point2(firstselected.x, firstselected.y), new Point2(secondselected.x, secondselected.y));
            if (Gamepanel.firstselected.x == Gamepanel.secondselected.x + 1)
            {
                while (Gamepanel.firstselected.Location.X != Gamepanel.firstselected.x * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X + 1, Gamepanel.firstselected.Location.Y);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X - 1, Gamepanel.secondselected.Location.Y);
                }
            }
            else if (Gamepanel.firstselected.x == Gamepanel.secondselected.x - 1)
            {
                while (Gamepanel.firstselected.Location.X != Gamepanel.firstselected.x * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X - 1, Gamepanel.firstselected.Location.Y);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X + 1, Gamepanel.secondselected.Location.Y);
                }
            }
            else if (Gamepanel.firstselected.y == Gamepanel.secondselected.y - 1)
            {
                while (Gamepanel.firstselected.Location.Y != Gamepanel.firstselected.y * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X, Gamepanel.firstselected.Location.Y - 1);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X, Gamepanel.secondselected.Location.Y + 1);
                }
            }
            else if (Gamepanel.firstselected.y == Gamepanel.secondselected.y + 1)
            {
                while (Gamepanel.firstselected.Location.Y != Gamepanel.firstselected.y * this.SIZE)
                {
                    Gamepanel.firstselected.Location = new Point(Gamepanel.firstselected.Location.X, Gamepanel.firstselected.Location.Y + 1);
                    Gamepanel.secondselected.Location = new Point(Gamepanel.secondselected.Location.X, Gamepanel.secondselected.Location.Y - 1);
                }
            }
            if (animalcontrol.GetEliminatedPoints().Count!=0)
            {
                foreach (Point2 point in animalcontrol.GetEliminatedPoints())
                {
                    animalpanels[point.X, point.Y].Dispose();
                    animalpanels[point.X, point.Y] = null;
                }
                firstselected = null;
                secondselected = null;
                animalcontrol.Regenerate();
                Adjust();
                mapImages(animalpanels, animalcontrol);
                Movedowmtimer.Start();
            }
            else
            {
                animalcontrol.swap(new Point2(firstselected.x, firstselected.y), new Point2(secondselected.x, secondselected.y));
                swapback2tiles();
            }
        }
        private void mapImages(Animalpanel[ , ] animalpanels, AnimalControl animalcontrol)
        {
            for(int i=0;i<=7;i++)
                for (int j = 0; j <= 7; j++)
                {
                    switch (animalcontrol.Animals[i,j].AnimalTypes)
                    {
                        case AnimalTypes.CAT:
                            animalpanels[i,j].BackgroundImage=global::Crazycrash.UI.Properties.Resources.deer;
                            break;
                        case AnimalTypes.DOG:
                            animalpanels[i,j].BackgroundImage=global::Crazycrash.UI.Properties.Resources.elef;
                            break;
                        case AnimalTypes.LION:
                            animalpanels[i,j].BackgroundImage=global::Crazycrash.UI.Properties.Resources.frog;
                            break;
                        case AnimalTypes.MONKEY:
                            animalpanels[i,j].BackgroundImage=global::Crazycrash.UI.Properties.Resources.hema;
                            break;
                        case AnimalTypes.PEGION:
                            animalpanels[i,j].BackgroundImage=global::Crazycrash.UI.Properties.Resources.panda;
                            break;
                    }
                }
        }
    }
}
